package com.fjec.market.rabbitmq.provider;

import com.fjec.common.protocol.FjecFruitsProto;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class ShopNumProvider {

    @Value("${fjec.market.shopnum.exchange.name}")
    private String exchangeName;

    @Value("${fjec.market.shopnum.router.key}")
    private String routingKey;

    @Resource(name = "shopNumAmqpTemplate")
    private RabbitTemplate shopNumAmqpTemplate;

    public void sendShopNumGen(Integer busiType, Long busiId, Integer markCode) {
        FjecFruitsProto.ShopNumGen shopNumGen = FjecFruitsProto.ShopNumGen.newBuilder()
                .setBusiType(busiType)
                .setBusiId(busiId)
                .setMarkCode(markCode)
                .build();
        FjecFruitsProto.FjecFruitsMsg msg = FjecFruitsProto.FjecFruitsMsg.newBuilder()
                .setShopNumGen(shopNumGen)
                .build();
        CorrelationData correlationData = new CorrelationData(busiType + "||" + busiId);
        shopNumAmqpTemplate.convertAndSend(exchangeName, routingKey, msg.toByteArray(), correlationData);
    }
}
